package com.company.cloud.user.modules.mgr.controller;
import java.util.function.Function;

import com.company.cloud.common.auth.annotation.StaffLogc;
import com.company.cloud.user.modules.cache.service.TeamCacheService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.company.cloud.common.auth.annotation.Authc;
import com.company.cloud.common.bean.BaseResult;
import com.company.cloud.common.bean.LoginSimpleUserInfo;
import com.company.cloud.common.constant.BaseConstant.EnabledEnum;
import com.company.cloud.common.constant.BaseReturnEnum;
import com.company.cloud.common.exception.BusinessException;
import com.company.cloud.common.utils.AuthContextUtils;
import com.company.cloud.common.utils.CheckUtils;
import com.company.cloud.user.modules.base.entity.TeamInfo;
import com.company.cloud.user.modules.base.service.TeamInfoService;
import com.company.cloud.user.modules.cache.models.TeamModel;
import com.company.cloud.user.modules.cache.utils.UserConvertUtils;
import com.company.cloud.user.modules.mgr.controller.req.TeamMgrQuery4PageParam;
import com.company.cloud.user.modules.mgr.controller.req.TeamMgrSaveParam;
import com.company.cloud.user.modules.mgr.controller.req.TeamMgrUpdateParam;
import com.company.cloud.user.modules.mgr.controller.req.TeamUserMgrAddParam;
import com.company.cloud.user.modules.mgr.controller.req.TeamUserMgrDelParam;
import com.company.cloud.user.modules.mgr.service.TeamMgrService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.log4j.Log4j2;

@Log4j2
@Authc
@RestController
@RequestMapping("/mgr/admin/team")
@Api(tags="User_Mgr_团队管理")
public class TeamMgrController {
	@Autowired
	private TeamInfoService teamInfoService;
	@Autowired
	private TeamMgrService teamMgrService;
	@Autowired
	private TeamCacheService teamCacheService;
	
	@PostMapping("/query")
	@ApiOperation(value="团队查询", response=TeamModel.class)
	public BaseResult query(@RequestBody TeamMgrQuery4PageParam params) {
		LoginSimpleUserInfo userInfo= AuthContextUtils.getUser();
		IPage<TeamInfo> page = teamInfoService.page(
				new Page<TeamInfo>(params.getPage(), params.getRows()), 
						new QueryWrapper<TeamInfo>()
							.eq("rows_state",  EnabledEnum.ENABLED.getCode())
							.eq("company_no", userInfo.getCompanyNo()) 
							.eq(CheckUtils.hasInt(params.getUserId()), "user_id", params.getUserId())
							.eq(StringUtils.hasText(params.getType()), "type", params.getType())
							.like(StringUtils.hasText(params.getName()), "name", params.getName())
				);
		return BaseResult.ok(teamCacheService.getTeamModelCache(page));
	}
	
	
	
	@GetMapping("/get")
	@ApiOperation(value="团队详情", response=TeamModel.class)
	public BaseResult get(@RequestParam("id")Integer id) {
		TeamInfo entity = teamInfoService.getById(id);
		if (entity == null || EnabledEnum.DISABLED.getCode().equals(entity.getRowsState())) {
			throw new BusinessException(BaseReturnEnum.DATA_NOT_EXISTS);
		}
		TeamModel teamModel = teamCacheService.getTeamModelCache(entity);
		teamModel = teamCacheService.getTeamModelWithTeamUser(teamModel);
		return BaseResult.ok(teamModel);
	}
	
	@PostMapping("/save")
	@StaffLogc(title = "User_Mgr_团队管理 、团队新增", action = "User_Mgr_团队管理、团队新增", actionAdo = "User_Mgr_团队管理")
	@ApiOperation(value="团队新增" ,response =BaseResult.class)
	public BaseResult save(@RequestBody TeamMgrSaveParam params) {
		teamMgrService.save(params, AuthContextUtils.getUser());
		return BaseResult.ok();
	}
	
	@PostMapping("/update")
	@StaffLogc(title = "User_Mgr_团队管理 、团队修改", action = "User_Mgr_团队管理、团队修改", actionAdo = "User_Mgr_团队管理")
	@ApiOperation(value="团队修改" ,response =BaseResult.class)
	public BaseResult update(@RequestBody TeamMgrUpdateParam params) {
		teamMgrService.update(params, AuthContextUtils.getUser());
		return BaseResult.ok();
	}
	
	@GetMapping("/delete")
	@StaffLogc(title = "User_Mgr_团队管理 、团队删除", action = "User_Mgr_团队管理、团队删除", actionAdo = "User_Mgr_团队管理")
	@ApiOperation(value="团队删除" ,response =BaseResult.class)
	public BaseResult delete(@RequestParam("id")Integer id) {
		teamMgrService.delete(id, AuthContextUtils.getUser());
		return BaseResult.ok();
	}
	
	@PostMapping("/addUser")
	@StaffLogc(title = "User_Mgr_团队管理 、团队新增用户", action = "User_Mgr_团队管理、团队新增用户", actionAdo = "User_Mgr_团队管理")
	@ApiOperation(value="团队新增用户" ,response =BaseResult.class)
	public BaseResult addUsers(@RequestBody TeamUserMgrAddParam params) {
		teamMgrService.addUsers(params, AuthContextUtils.getUser());
		return BaseResult.ok();
	}
	
	@PostMapping("/deleteUsers")
	@StaffLogc(title = "User_Mgr_团队管理 、团队删除用户", action = "User_Mgr_团队管理、团队删除用户", actionAdo = "User_Mgr_团队管理")
	@ApiOperation(value="团队删除用户" ,response =BaseResult.class )
	public BaseResult deleteUsers(@RequestBody TeamUserMgrDelParam params) {
		teamMgrService.delUsers(params,  AuthContextUtils.getUser());
		return BaseResult.ok();
	}
	
	
	
}
